package edu.istu.asu07_1.live.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import sun.awt.X11.Visual;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import edu.istu.asu07_1.live.configs.Constants;
import edu.istu.asu07_1.live.model.Model;
import edu.istu.asu07_1.live.model.TaktGenerator;

public class GameForm extends JFrame{

	private Vizualizator vizualizator;
	private DefaultFormBuilder builder;
	private CellConstraints cc;
	private NotifyPanel notifyPanel;

	public GameForm() {
		init();
		setLocationOnScreenCenter();
		setLookAndFeel();
		startGame();
		setVisible(true);
	}

	private void init(){
		setTitle("Artificial live");
		setMinimumSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		buildPanels();
	}

	private void setLocationOnScreenCenter(){
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		setLocation((screenSize.width/2)-DEFAULT_WIDTH/2, (screenSize.height/2)-DEFAULT_HEIGHT/2);
	}

	private void setLookAndFeel(){
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void buildPanels(){
		cc = new CellConstraints();
		vizualizator = new Vizualizator();

		FormLayout mainLayout = new FormLayout(
				"10px, 150px, fill:p:grow, p, fill:p:grow, 10px",
				"10px,fill:p:grow, fill:p, fill:p:grow, 10px"
		);
//		DefaultFormBuilder mainBuilder = new DefaultFormBuilder(mainLayout, new FormDebugPanel());
		DefaultFormBuilder mainBuilder = new DefaultFormBuilder(mainLayout);
		notifyPanel = new NotifyPanel();
		mainBuilder.add(notifyPanel.getContent(), cc.xywh(2, 2, 1, 3));
		mainBuilder.add(vizualizator.getContent(), cc.xy(4, 3));
		mainBuilder.add(genWhitePanel(), cc.xy(3, 2));
		mainBuilder.add(genWhitePanel(), cc.xy(5, 2));
		mainBuilder.add(genWhitePanel(), cc.xy(5, 3));
		mainBuilder.add(genWhitePanel(), cc.xy(5, 4));
		mainBuilder.add(genWhitePanel(), cc.xy(4, 2));
		mainBuilder.add(genWhitePanel(), cc.xy(4, 4));
		mainBuilder.add(genWhitePanel(), cc.xy(3, 3));

		mainBuilder.add(genWhitePanel(), cc.xy(3, 4));
		add(mainBuilder.getPanel());
	}

	private void startGame(){
		Model model = new Model();
		vizualizator.setModel(model);
		vizualizator.setHeight(Constants.FIELD_HEIGHT);
		vizualizator.setWidth(Constants.FIELD_WIDTH);
		TaktGenerator g = new TaktGenerator(model);
		g.getTaktablesList().add(vizualizator);
		g.getTaktablesList().add(notifyPanel);
		g.start();
	}

	private JPanel genWhitePanel(){
		JPanel p = new JPanel();
		p.setBackground(Color.WHITE);
		return p;
	}

	public static final int DEFAULT_WIDTH = 1024;
	public static final int DEFAULT_HEIGHT = 700;
}
